package ru.amse.tsyganov.jumleditor.main;


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import ru.amse.tsyganov.jumleditor.view.preferences.Images;

public class StartDialog extends Dialog {
	
	public static final int CANCEL = 0;
	
	public static final int OPEN_CHART = 1;
	
	public static final int NEW_ACTIVITY_CHART = 2;
	
	public static final int NEW_COLLABORATION_CHART = 3;

	private Shell sShell;
	
	private Button newActivityChartRadioButton;
	
	private Button newCollaborationChartRadioButton;
	
	private Button openChartRadioButton;
	
	private Button cancelButton = null;
	
	private Button okButton = null;
	
	private int status = OPEN_CHART;
	
	public StartDialog(Shell parent) {
		super(parent);
		
		sShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		
		createSShell();
		
		sShell.setLocation(parent.getLocation().x + parent.getSize().x / 2 - sShell.getSize().x / 2, 
				parent.getLocation().y + parent.getSize().y / 2 - sShell.getSize().y / 2);
	}
	
	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.marginBottom = 20;
		gridLayout.marginTop = 20;
		gridLayout.marginLeft = 10;
		gridLayout.marginRight = 20;
		
		sShell.setText("JUmlEditor");
		sShell.setLayout(null);
		sShell.setSize(220, 180);
		
		newActivityChartRadioButton = new Button(sShell, SWT.RADIO);
		newActivityChartRadioButton.setLocation(20, 20);
		newActivityChartRadioButton.setSize(200, 20);
		newActivityChartRadioButton.setText("New activity UML chart");
		newActivityChartRadioButton.setImage(Images.getActivityChartImage());
		
		newActivityChartRadioButton.addSelectionListener(new SelectionListener() {
			
			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}
			
			public void widgetSelected(SelectionEvent e) {
				status = NEW_ACTIVITY_CHART;
			}
			
		});
				
		newCollaborationChartRadioButton = new Button(sShell, SWT.RADIO);
		newCollaborationChartRadioButton.setLocation(20, 45);
		newCollaborationChartRadioButton.setSize(200, 20);
		newCollaborationChartRadioButton.setText("New collaboration UML chart");
		newCollaborationChartRadioButton.setImage(Images.getCollaborationChartImage());
		
		newCollaborationChartRadioButton.addSelectionListener(new SelectionListener() {
			
			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}
			
			public void widgetSelected(SelectionEvent e) {
				status = NEW_COLLABORATION_CHART;
			}
			
		});
		
		openChartRadioButton = new Button(sShell, SWT.RADIO);
		openChartRadioButton.setLocation(20, 70);
		openChartRadioButton.setSize(200, 20);
		openChartRadioButton.setText("Open UML chart");
		
		openChartRadioButton.addSelectionListener(new SelectionListener() {
			
			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}
			
			public void widgetSelected(SelectionEvent e) {
				status = OPEN_CHART;
			}
			
		});
		
		openChartRadioButton.setSelection(true);
		
		okButton = new Button(sShell, SWT.NONE);
		okButton.setText("Ok");
		okButton.setLocation(30, 100);
		okButton.setSize(70, 23);
		
		cancelButton = new Button(sShell, SWT.NONE);
		cancelButton.setText("Cancel");
		cancelButton.setLocation(120, 100);
		cancelButton.setSize(70, 23);
		
		okButton.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}

			public void widgetSelected(SelectionEvent e) {
				sShell.close();
				sShell.dispose();
			}
			
		});
		
		cancelButton.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}

			public void widgetSelected(SelectionEvent e) {
				status = CANCEL;
				sShell.close();
				sShell.dispose();
			}
			
		});
		
		okButton.setFocus();
	}
	
	public void open() {
		 sShell.open();
		 Display display = getParent().getDisplay();
		 while (!sShell.isDisposed()) {
		 	if (!display.readAndDispatch()) display.sleep();
		 }
	}

	public int getStatus() {
		return status;
	}
}
